

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <comedilib.h>
#include <earthworm.h>
#include <transport.h>
#include <trace_buf.h>
#include "adsend_comedi.h"
#include "getconfig.h"


ew_adsend_config_file*  GetConfig(char *configfile) {
  ew_adsend_config_file *returnval;
  
  returnval = malloc(sizeof(ew_adsend_config_file));

  returnval->Device = "/dev/comedi0";
  returnval->ChannelListLength = 3;
  returnval->SamplesPerChannel = 100;
  returnval->SamplesPerSecond = 100;
  returnval->PeriodNs = 10000000;

  returnval->ReadBufferSize = 500;

  returnval->ChannelList = malloc(3*sizeof(ChannelInfo));
  returnval->ChannelList[0].scn = malloc(sizeof(SCN));
  returnval->ChannelList[1].scn = malloc(sizeof(SCN));
  returnval->ChannelList[2].scn = malloc(sizeof(SCN));

  //East-west seismometer:
  strcpy(returnval->ChannelList[0].scn->sta, "LUC\0");
  strcpy(returnval->ChannelList[0].scn->comp, "EWS\0");
  strcpy(returnval->ChannelList[0].scn->net, "NI\0");
  returnval->ChannelList[0].scn->pin = 0;
  returnval->ChannelList[0].channel = 0;
  returnval->ChannelList[0].range = 3;
  returnval->ChannelList[0].aref = AREF_DIFF;

  //North-south seismometer: 
  strcpy(returnval->ChannelList[1].scn->sta, "LUC\0");
  strcpy(returnval->ChannelList[1].scn->comp, "NSS\0");
  strcpy(returnval->ChannelList[1].scn->net, "NI\0");
  returnval->ChannelList[1].scn->pin = 1;
  returnval->ChannelList[1].channel = 2;
  returnval->ChannelList[1].range = 3;
  returnval->ChannelList[1].aref = AREF_DIFF;
  //returnval->ChannelList[1].aref = AREF_GROUND;

  //Vertical seismometer: 
  strcpy(returnval->ChannelList[2].scn->sta, "LUC\0");
  strcpy(returnval->ChannelList[2].scn->comp, "ZZS\0");
  strcpy(returnval->ChannelList[2].scn->net, "NI\0");
  returnval->ChannelList[2].scn->pin = 2;
  returnval->ChannelList[2].channel = 4;
  returnval->ChannelList[2].range = 3;
  returnval->ChannelList[2].aref = AREF_DIFF;
  //returnval->ChannelList[2].aref = AREF_GROUND;

  returnval->ModuleId =malloc(13*sizeof(char));
  strcpy(returnval->ModuleId, "MOD_ADSEND_A");

  returnval->OutputRing = malloc(10*sizeof(char));
  strcpy(returnval->OutputRing, "WAVE_RING");

  returnval->InstallationId = malloc(4*sizeof(char));
  strcpy(returnval->InstallationId, "LUC");
  
  //Set up the msg logo:
  unsigned char instid;
  unsigned char moduleid;
  	
  if(GetLocalInst(&instid)<0) {
      logit("ew", "adsend_comedi: could not get local installation id.\n");
  }
  if(GetModId(returnval->ModuleId, &moduleid)==-1) {
      logit("ew", "Could not get module id.\n");
  }
  returnval->msg_logo.instid = instid;
  returnval->msg_logo.mod = moduleid;
  GetType("TYPE_TRACEBUF", &returnval->msg_logo.type);

  return returnval;
}


 /***********************************************************************
  *                              LogConfig()                            *
  *                                                                     *
  *                   Log the configuration parameters                  *
  ***********************************************************************/
void LogConfig(ew_adsend_config_file *cf){
  //printf("Device: %s\n",cf->Device);
  //printf("Channel List Length: %d\n", cf->ChannelListLength);
  //printf("Samples Per Channel: %d\n", cf->SamplesPerChannel);
  //printf("Samples Per Second: %d\n", cf->SamplesPerSecond);
  return;
}



